//
//  VowListViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/11/11.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "VowListViewController.h"
#import "CollectionViewCell.h"
#import "CollectionViewHeaderView.h"
#import "ThirdCateModle.h"
#import "SecondCateModle.h"
#import "LeftTableViewCell.h"
#import "LJCollectionViewFlowLayout.h"
#import "VowShopListViewController.h"
#import "BannerModel.h"
@interface VowListViewController ()<UISearchBarDelegate,
UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate,
UICollectionViewDataSource>

@property (nonatomic, strong) UITableView      * tableView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic ,strong) SecondCateModle  * modle;
@property (nonatomic ,strong) BannerModel      * banModle;
@property (nonatomic, strong) NSMutableArray   * dataSource;
@property (nonatomic, strong) NSMutableArray   * banArray;
@property (nonatomic, strong) NSMutableArray   *collectionDatas;
@property (nonatomic, strong) NSMutableArray   *ThirdMunuArray;
@property (nonatomic, strong) NSString         * crateID;
@property (nonatomic, strong) UILabel * leftlabel;
@property (nonatomic, strong) UIImageView*msgImg;
@property (nonatomic ,strong) UIView *blankView;

@end

@implementation VowListViewController

{
    NSInteger _selectIndex;
    BOOL _isScrollDown;
}


-(void)viewDidLoad{
    [super viewDidLoad];
    self.title=MyString(@"分类");
    _selectIndex = 0;
    _isScrollDown = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.collectionView];
    self.blankView=[ZJUITools showBlankView];
    self.blankView.hidden=YES;
    [self.view addSubview:self.blankView];
    [self creatNavUI];
    if (self.isMatch==YES) {
        [self loadShopData];
    }else{
        [self loadShopDatalist];
    }
    
    //[self.tableView reloadData];
    //[self.collectionView reloadData];
}
#pragma mark - Getters
-(NSMutableArray *)banArray{
    if (!_banArray)
    {
        _banArray = [NSMutableArray array];
    }
    return _banArray;
}
- (NSMutableArray *)dataSource
{
    if (!_dataSource)
    {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (NSMutableArray *)collectionDatas
{
    if (!_collectionDatas)
    {
        _collectionDatas = [NSMutableArray array];
    }
    return _collectionDatas;
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 100, MainH)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.rowHeight = 50;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorColor = SeparatorColor;
        _tableView.separatorInset=UIEdgeInsetsZero;
        _tableView.backgroundColor=Gray_Color;
        [_tableView registerClass:[LeftTableViewCell class] forCellReuseIdentifier:kCellIdentifier_Left];
    }
    return _tableView;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        LJCollectionViewFlowLayout *flowlayout = [[LJCollectionViewFlowLayout alloc] init];
        //设置滚动方向
        [flowlayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        //左右间距
        flowlayout.minimumInteritemSpacing = 10;
        //上下间距
        flowlayout.minimumLineSpacing =10 ;
        [flowlayout setHeaderReferenceSize:CGSizeMake(MainW - 110,125)];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake( 100, 0, MainW-100, MainH-64) collectionViewLayout:flowlayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView setBackgroundColor:[UIColor whiteColor]];
        //注册cell
        [_collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:kCellIdentifier_CollectionView];
        //注册分区头标题
        [_collectionView registerClass:[CollectionViewHeaderView class]
            forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                   withReuseIdentifier:@"CollectionViewHeaderView"];
    }
    return _collectionView;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 15, 0, 10);//分别为上、左、下、右
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section==-100) {
        return CGSizeMake(MainW - 110,150);
    }else{
        return CGSizeMake(MainW - 110,30);
    }
    
}
#pragma mark - UITableView DataSource Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_Left forIndexPath:indexPath];
    //SecondCateModle *model = self.dataSource[indexPath.row];
    if (indexPath.section==0) {
        cell.selected=YES;
    }
    cell.name.text =self.dataSource[indexPath.row][@"name"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _selectIndex = indexPath.row;
    self.crateID=self.dataSource[indexPath.row][@"cateId"];
    [self loadViewSecondShopList];
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.collectionDatas.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    SecondCateModle*model=self.collectionDatas[section];
    return model.children.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier_CollectionView forIndexPath:indexPath];
    SecondCateModle * model = self.collectionDatas[indexPath.section];
    self.ThirdMunuArray = [ThirdCateModle mj_objectArrayWithKeyValuesArray:model.children];
    ThirdCateModle * thirdModel = self.ThirdMunuArray[indexPath.row];
    cell.name.text=thirdModel.name;
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",thirdModel.icon]] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SecondCateModle * model = self.collectionDatas[indexPath.section];
    self.ThirdMunuArray = [ThirdCateModle mj_objectArrayWithKeyValuesArray:model.children];
    ThirdCateModle * thirdModel = self.ThirdMunuArray[indexPath.row];
    VowShopListViewController*vc=[[VowShopListViewController alloc]init];
    vc.ids=thirdModel.cateId;
    vc.isMatch=self.isMatch;
    vc.popWhere=self.popWhere;
    [self.navigationController pushViewController:vc animated:YES];
    NSLog(@"第_----------%@级",vc.ids);
}
- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((MainW-180)/3,(MainW-180)/3+30);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath
{
    NSString *reuseIdentifier;
    if ([kind isEqualToString:UICollectionElementKindSectionHeader])
    { // header
        reuseIdentifier = @"CollectionViewHeaderView";
    }
    CollectionViewHeaderView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                                        withReuseIdentifier:reuseIdentifier
                                                                               forIndexPath:indexPath];
    if ([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        NSMutableArray*imag=[NSMutableArray array];
        for (BannerModel *model in self.banArray) {
            NSString *string=model.imageUrl;
            [imag addObject:string];
        }
        if (indexPath.section==-10) {
            view.cycleScrollerView.hidden=NO;
            view.cycleScrollerView.imageURLStringsGroup = imag;  // 网络图片
            view.title.frame=CGRectMake(10, 125, MainW-120, 20);
        }else{
            view.cycleScrollerView.hidden=YES;
            view.title.frame=CGRectMake(10, 5, MainW-120, 20);
        }
        SecondCateModle*modle=self.collectionDatas[indexPath.section];
        view.title.text = modle.name;
        //NSLog(@"名称:%@",model.name);
    }
    return view;
}

-(void)loadShopData{
    NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    [self doHttpRequestWithApi:cateTree method:GET dic:dic successBlock:^(NSDictionary *dic) {
        NSLog(@"%@",dic[@"data"]);
        self.dataSource=dic[@"data"];
        if (self.dataSource.count==0) {
            self.blankView.hidden=NO;
        }else{
            self.blankView.hidden=YES;
            [self.tableView reloadData];
            [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
            self.crateID=self.dataSource[0][@"cateId"];
            if (self.crateID!=nil) {
                [self loadViewSecondShopList];
            }
            
            
        }
    }];
}
-(void)loadShopDatalist{
    NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];
    dic[@"ids"]=self.prayString;
    [self doHttpRequestWithApi:praycateTree method:GET dic:dic successBlock:^(NSDictionary *dic) {
        NSLog(@"%@",dic[@"data"]);
        self.dataSource=dic[@"data"];
        if (self.dataSource.count==0) {
            self.blankView.hidden=NO;
        }else{
            self.blankView.hidden=YES;
            [self.tableView reloadData];
            [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
            self.crateID=self.dataSource[0][@"cateId"];
            if (self.crateID!=nil) {
                [self loadViewSecondShopList];
            }
            
            
        }
    }];
}
-(void)loadViewSecondShopList{
    NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];
    dic[@"cateId"]=self.crateID;
    [self doHttpRequestWithApi:API_SECONDCLASS_LIST method:GET dic:dic successBlock:^(NSDictionary *dic) {
        self.collectionDatas=[SecondCateModle mj_objectArrayWithKeyValuesArray:dic[@"data"][@"children"]];
        //self.banArray=[BannerModel mj_objectArrayWithKeyValuesArray:dic[@"data"][@"adverts"]];
        [self.collectionView reloadData];
        //NSLog(@"  二级********%@",self.collectionDatas);
    }];
}

@end
